package com.wang;

import com.wang.bean.Result;
import com.wang.config.ShiroUtils;
import com.wang.util.*;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER;

@Controller
@RequestMapping("/")
public class LoginController {

    //@Autowired
    //StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/")
    public String index()
    {
        return "admin/login";
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    public Result userLogin (
            @RequestParam(value = "userName") String userName,
            @RequestParam(value = "passWord") String passWord){     //, ModelMap map

        Result result = new Result();

        try{
            Subject subject = ShiroUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(userName, passWord);
            subject.login(token);
            System.out.println("getSession:"+subject.getSession().getId());
            LOGGER.info("登录成功");
            //return "redirect:task/singlechoiceadd";
            result.setCode("100");
            //Map<String,String> map = new HashMap<>();
            //map.put("token",());
            result.setData((subject.getSession().getId()).toString());
            return result;
        }

        catch (Exception e) {
            // e.printStackTrace();
            //return "admin/login";
            System.out.println(e.toString());
            //map.addAttribute("code","-1000");
            //map.addAttribute("message","账号或密码不正确");
            //return "admin/login";
            result.setCode("101");
            return result;
        }
    }


    /**
     * 退出登陆
     */
    @RequestMapping("/logout")
    public String logout()
    {
        ShiroUtils.logout();
        System.out.println("ShiroUtils.logout");
        return "admin/login";
    }

    @RequestMapping("/upfile")
    public String upfile()
    {
        return "admin/upfile";
    }

    @RequestMapping("/login")
    public String login()
    {
        return "admin/login";
    }
}

